package org.jivesoftware.smack.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.SocketFactory;
import org.jivesoftware.smack.util.StringUtils;

class HTTPProxySocketFactory extends SocketFactory
{
  private ProxyInfo proxy;
  private static final Pattern RESPONSE_PATTERN = Pattern.compile("HTTP/\\S+\\s(\\d+)\\s(.*)\\s*");

  public HTTPProxySocketFactory(ProxyInfo paramProxyInfo)
  {
    this.proxy = paramProxyInfo;
  }

  public Socket createSocket(String paramString, int paramInt)
    throws IOException, UnknownHostException
  {
    return httpProxifiedSocket(paramString, paramInt);
  }

  public Socket createSocket(String paramString, int paramInt1, InetAddress paramInetAddress, int paramInt2)
    throws IOException, UnknownHostException
  {
    return httpProxifiedSocket(paramString, paramInt1);
  }

  public Socket createSocket(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    return httpProxifiedSocket(paramInetAddress.getHostAddress(), paramInt);
  }

  public Socket createSocket(InetAddress paramInetAddress1, int paramInt1, InetAddress paramInetAddress2, int paramInt2)
    throws IOException
  {
    return httpProxifiedSocket(paramInetAddress1.getHostAddress(), paramInt1);
  }

  private Socket httpProxifiedSocket(String paramString, int paramInt)
    throws IOException
  {
    String str1 = this.proxy.getProxyAddress();
    int i = this.proxy.getProxyPort();
    Socket localSocket = new Socket(str1, i);
    String str2 = "CONNECT " + paramString + ":" + paramInt;
    String str4 = this.proxy.getProxyUsername();
    String str3;
    if (str4 == null)
    {
      str3 = "";
    }
    else
    {
      localObject = this.proxy.getProxyPassword();
      str3 = "\r\nProxy-Authorization: Basic " + StringUtils.encodeBase64(new StringBuilder(String.valueOf(str4)).append(":").append((String)localObject).toString());
    }
    localSocket.getOutputStream().write((str2 + " HTTP/1.1\r\nHost: " + str2 + str3 + "\r\n\r\n").getBytes("UTF-8"));
    Object localObject = localSocket.getInputStream();
    StringBuilder localStringBuilder = new StringBuilder(100);
    int j = 0;
    do
    {
      int k = (char)((InputStream)localObject).read();
      localStringBuilder.append(k);
      if (localStringBuilder.length() > 1024)
        throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Recieved header of >1024 characters from " + str1 + ", cancelling connection");
      if (k == -1)
        throw new ProxyException(ProxyInfo.ProxyType.HTTP);
      if (((j == 0) || (j == 2)) && (k == 13))
        j++;
      else if (((j == 1) || (j == 3)) && (k == 10))
        j++;
      else
        j = 0;
    }
    while (j != 4);
    if (j != 4)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Never received blank line from " + str1 + ", cancelling connection");
    String str5 = localStringBuilder.toString();
    BufferedReader localBufferedReader = new BufferedReader(new StringReader(str5));
    String str6 = localBufferedReader.readLine();
    if (str6 == null)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Empty proxy response from " + str1 + ", cancelling");
    Matcher localMatcher = RESPONSE_PATTERN.matcher(str6);
    if (!localMatcher.matches())
      throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Unexpected proxy response from " + str1 + ": " + str6);
    int m = Integer.parseInt(localMatcher.group(1));
    if (m != 200)
      throw new ProxyException(ProxyInfo.ProxyType.HTTP);
    return localSocket;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.proxy.HTTPProxySocketFactory
 * JD-Core Version:    0.6.1
 */